<?php

/**
 * Class CacheFacade
 */
final class CacheFacade{
    /**
     * @var
     */
    private static $instance;
    /**
     * @var
     */
    private $cacher;
    /**
     * @var array
     */
    private $cachers = array(
        'Memcached' => 'MemcachedCacher',
        'SQLite3' => 'SqliteCacher',
    );

    /**
     *
     */
    private function __construct() {
        foreach ($this->cachers as $type => $class) {
            if (class_exists($type) && class_exists($class)) {
                $this->cacher = new $class();
                break;
            }
        }
    }

    /**
     * @return CacheFacade
     */
    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     *
     */
    private function __clone() {}

    /**
     * @param $key
     * @return null
     */
    public function get($key)
    {
        if (!$this->cacher) {
            return null;
        }

        return $this->cacher->get($key);
    }

    /**
     * @param $key
     * @param $value
     * @param int $time
     * @return null
     */
    public function set($key, $value, $time = 0)
    {
        if (!$this->cacher) {
            return null;
        }

        $this->cacher->set($key, $value, $time);
    }
}
